package xyz.bali16.module.article.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import xyz.bali16.application.core.enums.ApiEnum;
import xyz.bali16.application.core.model.Result;
import xyz.bali16.module.article.entity.Article;
import xyz.bali16.module.article.entity.Like;
import xyz.bali16.module.article.model.like.LikeAddParam;
import xyz.bali16.module.article.model.like.LikePageParam;
import xyz.bali16.module.article.service.ArticleService;
import xyz.bali16.module.article.service.LikeService;

import javax.validation.Valid;

import java.util.List;

import static xyz.bali16.application.core.model.Result.*;
import static xyz.bali16.module.article.utils.ResultUtils.errorLog;



@RestController
@RequestMapping(BaseController.API+"like")
@Api(tags = "点赞接口")
@AllArgsConstructor
public class LikeController extends BaseController{
    public static final Logger logger = LoggerFactory.getLogger(LikeController.class);
    @Autowired
    LikeService likeService;
    @Autowired
    ArticleService articleService;
    @GetMapping
    @ApiOperation("分页条件查询点赞")
    //@PreAuthorize("@auth.permission('plugin:article:like:query')")
    public Result<IPage<Like>> pageQuery(@Valid LikePageParam param) {
        IPage<Like> likeIPage = likeService.getPage(param);
        List<Like> likes = likeIPage.getRecords();
        for(Like like : likes){
            Article article = articleService.getById(like.getTargetId());
            like.setArticleTitle(article.getTitle());
            like.setArticleContent(article.getContent());
        }
        return success(ApiEnum.GET_SUCCESS, likeIPage);
    }

    @PostMapping
    //@PreAuthorize("@auth.permission('plugin:article:like:add')")
    @ApiOperation("添加点赞")
    public Result<String> add(@RequestBody @Valid LikeAddParam param) {
        try {
            likeService.addLike(param);
            return response(ApiEnum.ADD_SUCCESS);
        } catch (Exception e) {
            errorLog(logger, e, "新增点赞 信息失败 {}", e.getMessage());
            return failure(ApiEnum.ADD_ERROR, "新增点赞失败", e);
        }
    }
    @GetMapping("/count")
    //@PreAuthorize("@auth.permission('plugin:article:like:query')")
    @ApiOperation("获取点赞数量")
    public Result<Integer> count() {
        try {
            return success(ApiEnum.GET_SUCCESS,likeService.count());
        } catch (Exception e) {
            return failure(ApiEnum.ADD_ERROR, "查询点赞数量失败", e);
        }
    }
    @DeleteMapping("/{likeId}")
    //@PreAuthorize("@auth.permission('plugin:article:like:delete')")
    @ApiOperation("删除点赞")
    @ApiImplicitParam(name = "likeId", value = "点赞id", paramType = "path", required = true)
    public Result<String> deleteLike(@PathVariable("likeId") String likeId) {
        try {
            likeService.delete(likeId);
            return response(ApiEnum.DELETE_SUCCESS);
        } catch (Exception e) {
            errorLog(logger, e, "删除点赞 '{}' 失败. {}", likeId, e.getMessage());
            return failure(ApiEnum.DELETE_ERROR, e);
        }
    }

}
